package com.shuzijun.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

public class PalindromePartitioning{
    public static void main(String[] args) {
         
         Solution solution = new PalindromePartitioning().new Solution();
        
        solution.partition("aab");
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        private ArrayList<List<String>> result = new ArrayList<>();
    public List<List<String>> partition(String s) {
        backTrack(s,0,new ArrayList<>());
        return result;
    }

        private void backTrack(String s, int k, ArrayList<Object> path) {
            if (k == s.length()){
                result.add(new ArrayList(path));
                return;
            }

            for (int end = k; end < s.length(); end++) {
                if (isPalindRome(s,k,end)){
                    path.add(s.substring(k,end+1));
                    backTrack(s,end+1,path);
                    path.remove(path.size()-1);
                }
            }
        }

        private boolean isPalindRome(String s, int p, int r) {
            int i = p;
            int j = r;
            while (i <= j){
                if (s.charAt(i) != s.charAt(j)) return false;
                i++;
                j--;
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}